import logging
import re

import requests


def 获取违规并上传ERP(page, shop_name):
    trs = page.eles('.auxo-table-row auxo-table-row-level-0', timeout=2)
    for tr in trs:
        罚单编号 = tr.child(1).text
        处罚状态 = tr.child(3).text
        罚款积分 = re.findall(r'(\d+\.*\d*)', tr.child(7).text)[0]
        违规时间 = tr.child(8).text.replace('\n', ' ')
        tr.child(9).ele('text=处罚详情').click(by_js=True)
        page.wait(1)
        if page.s_ele('text=展开'):
            page('text=展开').click(by_js=True)
            page.wait(0.5)
        平台违规判定信息 = page('.feedback-tooltip-target ').text
        data = [{
            'ViolationOrderNo': 罚单编号,
            'PlatformPunishState': 2,
            'PunishAmount': 罚款积分,
            'ViolationTime': 违规时间,
            'Platform': 6,  # 抖音平台枚举
            'ShopName': shop_name,
            'PlatformPunishMsg': 平台违规判定信息
        }]
        url = 'http://192.168.16.240:8001/api/Customerservice/UnPayOrder/BatchAddPlatformPunishAsync'
        # url = 'http://95qmvu.natappfree.cc/api/Customerservice/UnPayOrder/BatchAddPlatformPunishAsync'
        while True:
            res = requests.post(url, json=data)
            if res.status_code == 200:
                break
        logging.info(res.text)
    page.close()